package com.gastronomy.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gastronomy.constant.Constant;
import com.gastronomy.entity.User;
import com.gastronomy.service.UserService;

@Controller
@RequestMapping("/login")
public class LoginController {

	@Resource
	private UserService userService;
	
	@RequestMapping("/login")
	public String toLogin(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		System.out.println("登陆");
		return "/loginUser/login";
	}
	
	
	@RequestMapping("/error")
	public String toError(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		System.out.println("错误");
		return "/loginUser/error";
	}
	
	@RequestMapping("/reregist")
	public String reRegist(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		System.out.println("注册也main");
		return "/loginUser/reregist";
	}
	
	@RequestMapping("/toRegist")
	@ResponseBody
	public Map<String, String> regist( User user,HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String roleId = "1";
		String password2 = request.getParameter("passwordT");
		
		return userService.addUser(user,roleId,password2,null);
	}
	
	@RequestMapping("/reregistBusi")
	public String reRegistBusi(HttpServletRequest request,
			HttpServletResponse response, Model model) {

		System.out.println("注册也main");
		return "/loginUser/reregistBusi";
	}
	
	@RequestMapping("/toRegistBusi")
	@ResponseBody
	public Map<String, String> registBusi( User user,HttpServletRequest request,
			HttpServletResponse response,MultipartFile picFile, Model model) {
		
		String roleId = "3";
		String password2 = request.getParameter("passwordT");
		
		return userService.addUser(user,roleId,password2,picFile);
	}
	
	@RequestMapping("/toLogin")
	@ResponseBody
	public Map<String, String> login( User user,HttpServletRequest request,
			HttpServletResponse response, Model model) {
		
		Map<String, String> map =  userService.queryUser(user);
//		System.out.println("-----------------进入测试");
//		System.out.println(user.getRole().getId());
		if(map.get("loginResult").equals("SUCCESS")){
			HttpSession session=request.getSession();
			user = userService.getUserByLoginName(user.getUserName());
			session.setAttribute(Constant.SESSION_USER, user);
		}
		return map;
	}
	
	@RequestMapping("/outLand")
	@ResponseBody
	public Map<String, String> outLand(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Map<String, String> resultMap = new HashMap<String, String>();
		 HttpSession session=request.getSession();  
		 session.setAttribute(Constant.SESSION_USER, null);
		
		 resultMap.put("outLand", "SUCCESS");
		 return resultMap;
	}
	
}
